<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "basemodule".
 *
 * @property int $Id 模块标识
 * @property int $Pid 上级标识
 * @property int $ClassId 模块类别
 * @property string $Title 模块名称
 * @property string $Style 模块描述
 * @property int $Nullity 是否启用
 * @property string $RouteURL 跳转地址
 * @property string $Icon 图标样式
 * @property int $SortId 排序标识
 * @property string $Options 其他选项
 * @property string $PathRoute 模块路径
 * @property string $TabId 唯一标识
 */
class Basemodule extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'basemodule';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['Pid', 'ClassId', 'SortId', 'Nullity'], 'integer'],
            [['Title'], 'required'],
            [['Title', 'TabId'], 'string', 'max' => 50],
            [['Style', 'Icon'], 'string', 'max' => 128],
            [['RouteURL', 'Options'], 'string', 'max' => 256],
            [['PathRoute'], 'string', 'max' => 1024],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'Id' => 'ID',
            'Pid' => 'Pid',
            'ClassId' => 'Class ID',
            'Title' => 'Title',
            'Style' => 'Style',
            'Nullity' => 'Nullity',
            'RouteURL' => 'Route Url',
            'Icon' => 'Icon',
            'SortId' => 'Sort ID',
            'Options' => 'Options',
            'PathRoute' => 'Path Route',
            'TabId' => 'Tab ID',
        ];
    }

    public function getNode($roleid)
    {
        $sql = "select *
                from basemodule";
        $data = yii::$app->db->createCommand($sql)->queryAll();
        return self::_generateTree($data);
    }

    private static function _generateTree ($data, $pid = 0) {
        $tree = [];
        if ($data && is_array($data)) {
            foreach($data as $k => $v) {
                if($v['Pid'] == $pid) {
                    $li = empty($v['Nullity']) ? "<i class='fa fa-check-circle text-success'></i>" : "<i class='fa fa-minus-circle text-rose'></i>" ;
                    $tags = empty($v['ClassId']) ? "<i class='text-primary'>系统模块</i>" : (($v['ClassId'] == 1) ? "<i class='text-rose'>导航菜单</i>" : "<i class='text-gray'>执行动作</i>");
                    $ids = $v['Id'];
                    //$delete = '<a href=/module/handler-module?ids='.$ids.'>删除</a>';
                    $delete = '<a href="javascropt:void(0)" id="btn_trash" data-ids='.$ids.' style="color: #f44336;">删除</a>';
                    $tree[] = [
                        'id' => $v['Id'],
                        'pid' => $v['Pid'],
                        'href' => $v['RouteURL'],
                        'ClassId' => $v['ClassId'],
                        'text' => $v['Title'],
                        'nodes' => self::_generateTree($data, $v['Id']),
                        'tags' => [$delete, $li, $tags],
                    ];
                }
            }
        }
        if (empty($tree)){
            $tree = '';
        }
        return $tree;
    }


    public function getModule($id)
    {
        return self::findOne(['Id' => $id]);
    }


    public function updateModule($req)
    {
        if (isset($req["txt_PID"])){
            //修改
            $module = self::findOne(['Id' => $req["txt_ID"]]);
            $module->Id = empty($req["txt_ID"]) ? 0 : $req["txt_ID"];
            $module->Nullity = (isset($req["txt_NULLITY"]) && !empty($req["txt_NULLITY"])) ? 1 : 0;
            if (empty($req["txt_ID"])){
                return ['statusCode'=>300, 'message'=>"非法操作"];
            }
        }else{
            //添加
            $module = new Basemodule();
            $module->TabId = md5(yii::$app->getSecurity()->generateRandomString());
            $module->Pid = empty($req["txt_ID"]) ? 0 : $req["txt_ID"];
            $module->Nullity = isset($req["txt_NULLITY"]) ? 0 : 1;
        }
        $module->ClassId = empty($req["txt_CLASSID"]) ? 0 : $req["txt_CLASSID"];
        $module->Title = $req["txt_TITLE"];
        $module->RouteURL = $req["txt_ROUTEURL"];
        $module->Icon = $req["txt_ICON"];
        $module->Style = $req["txt_STYLE"];
        $module->Options = $req["txt_OPTIONS"];
        $module->SortId = empty($req["txt_SORTID"]) ? 0 : $req["txt_SORTID"];
        $result = $module->save();
        if ($result){
            return ['statusCode'=>200, 'message'=>"已成功保存系统模块信息"];
        }
        return ['statusCode'=>300, 'message'=> "保存系统模块信息失败"];
    }


    public function delModule($data)
    {
        $ids = explode(',', $data['ids']);
        if (empty($ids)) return ['statusCode'=>300, 'message'=>'请选中需要操作模块信息'];
        $qurey = self::deleteAll(['in', 'Id', $ids]);
        if ($qurey){
            return ['statusCode'=>200, 'message'=>'已成功删除系统模块信息'];
        }
        return ['statusCode'=>300, 'message'=>'删除系统模块信息失败'];
    }

    /**
     * 获取角色下执行操作的权限
     */
    public static function getMoreRole($roleId, $moduleId)
    {
        $sql = "select * from basemodule where Pid = {$moduleId} and Id in (select ModuleId from baseprivilege where RoleId = {$roleId}) order by SortId asc";
        $citem = yii::$app->db->createCommand($sql)->queryAll();
        return $citem;
    }


}
